using Identity.Application.Contracts.Common;
using Identity.Application.Contracts.Services;
using Identity.Application.Users.Commands;
using MediatR;

namespace Identity.Application.Users.CommandHandler;

public class ChangePasswordCommandHandler : IRequestHandler<ChangePasswordCommand, Result>
{
    private readonly IUserService _userService;

    public ChangePasswordCommandHandler(IUserService userService)
    {
        _userService = userService;
    }
    public async Task<Result> Handle(ChangePasswordCommand request, CancellationToken cancellationToken)
    {
        var result = await _userService.CheckPasswordAsync(request.Id, request.currentPassword);
        if (!result.Succeeded) return Result.Failure(result.Error ?? "密码错误");

        var changeResult = await _userService.ChangePasswordAsync(request.Id, request.currentPassword, request.newPassword);
        if (!changeResult.Succeeded) return Result.Failure(changeResult.Error ?? "修改密码失败！");

        return Result.Success();

    }
}

        
    